package com.example.demo.controller;

import com.example.demo.entity.UserInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-05-18
 * Time: 8:39
 */
// 等同于 @ResponseBody 和 @Controller 注解
@RestController // 让 Spring 框架启动时加载, 并且返回的非页面数据
public class UserController {
    @RequestMapping(value = "/user1", method = RequestMethod.POST)
    public String fun1() {
        return "这是 user1 路由下的 fun1 方法";
    }

    @PostMapping("/user2")
    public String fun() {
        return "这是 user2 路由下的 Post治党路由方法";
    }

    @RequestMapping("/user3")
    public String getParameter(String name) {
        return "这是获取到的参数 : " + name;
    }

    @RequestMapping("/user4")
    public String getParameter1(HttpServletRequest request, HttpServletResponse response) {
        return "这是获取到的参数 : " + request.getParameter("name");
    }

    @RequestMapping("/user5")
    public String getParameter1(String name, String password) {
        return "获取到的 name : " + name + " password : " + password;
    }

    @RequestMapping("/reg")
    public Object reg(UserInfo userInfo) {
        System.out.println(userInfo);
        return userInfo;
    }

    @RequestMapping("/html")
    public Object reg1() {
        return "<h1> 这是一个 HTML 标签</h1>";
    }

    @RequestMapping("/reg1")
    public Object reg1(@Value("username") String name, String password) {
        return "name : " + name + " password : " + password;
    }

    @RequestMapping("/reg2")
    public Object reg2(@RequestParam(value = "username", required = false) String name, String password) {
        return "name : " + name + " password : " + password;
    }

    @RequestMapping("/reg3")
    public Object reg3(@RequestBody UserInfo userInfo) {
        return userInfo;
    }

    @RequestMapping("/reg4/{name}/{password}")
    public String reg3(@PathVariable(value = "name") String username, @PathVariable String password) {
        return "username : " + username + " password : " + password;
    }

    @RequestMapping("/upload1")
    public String upLoad1(@RequestParam("myimg") MultipartFile file) throws IOException {
        File saveFile = new File("E:\\Javacode\\spring\\spring-mvc\\myimg.png"); // 文件保存的路径
        try {
            file.transferTo(saveFile); // 上传文件 - 本身是没有返回值的, 通过 try catch 来检验是否上传成功
            return "文件上传成功! ";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "文件上传失败 !";

    }

    @RequestMapping("upload2")
    public String upload2(@RequestParam("myimg") MultipartFile file) {
        // 创建文件名名称
        String fileName = UUID.randomUUID() + //文件名
                file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); // 后缀名
        // lastIndexOf 左闭右开需要注意, 包含分割后的 . 的
        File saveFile = new File("E:\\Javacode\\spring\\spring-mvc\\" + fileName); // 保存文件
        try {
            file.transferTo(saveFile); // 上传文件
            return "上传文件成功 !";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传文件失败";
    }

    @RequestMapping("getCK")
    public void getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
    }

    @RequestMapping("getCK2")
    public String getCookie(@CookieValue(value = "spring", required = false) String spring) {
        return spring;
    }

    @RequestMapping("getHD")
    public String getHeader(@RequestHeader("Host") String host) {
        return host;
    }

    private static final String SESSION_KEY = "DEFAULT_VALUE";

    @RequestMapping("/createSession")
    public void CreateSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true); // 没有 session 就创建 session
        session.setAttribute(SESSION_KEY, "username"); // 将username存入到会话中
    }

    @RequestMapping("/getSession")
    public String getSession2(HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 有则获取, 没有就不建立

        return " Servlet 获取 : " + (String) session.getAttribute(SESSION_KEY);
    }

    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(SESSION_KEY) String value) {
        return "注解获取 : " + value;
    }

    @RequestMapping("/resJson")
    public HashMap<String, String> resJson() {
        HashMap<String, String> map = new HashMap<>();
        map.put("zhangsan", "1");
        map.put("lisi", "2");
        return map;
    }

}
